package edu.kit.aifb.ldstream.learner.util;

public final class RecursiveSum {

	private RecursiveSum(){}
	
	public static double recursiveSum(double[] values){
		if(values.length == 0){
			throw new IllegalArgumentException("Array size must be greater than 0");
		}
		if(values.length == 1){
			return values[0];
		}
		return recursiveSum(values, 0, values.length - 1);
	}
	
	public static double recursiveSum(double[] values, int min, int max){
		
		if(max - min < 0){
			return 0d;
		}
		if(max - min == 0){
			return values[min];
		}
		if(max - min == 1){
			return values[min] + values[max];
		}
		int middle = (max + min) / 2;
		return recursiveSum(values, min, middle) + recursiveSum(values, middle + 1, max);
		
	}
}
